package com.vf.activity.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.github.pagehelper.PageInfo;
import com.vf.activity.domain.DTO.ActDTO;
import com.vf.activity.domain.DTO.Activities2DTO;
import com.vf.activity.domain.DTO.ActivitiesDTO;
import com.vf.activity.domain.DTO.ActivityTypeDTO;
import com.vf.activity.domain.VO.ActivityVO;
import com.vf.activity.domain.VO.AddActivityVO;
import com.vf.activity.mapper.ActivityAppMapper;
import com.vf.common.constant.HttpStatus;
import com.vf.common.core.page.TableDataInfo;
import com.vf.common.utils.SplitLocationUtil;
import com.vf.common.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.vf.activity.mapper.ActivitiesMapper;
import com.vf.activity.domain.Activities;
import com.vf.activity.service.IActivitiesService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 活动Service业务层处理
 * 
 * @author LBS
 * @date 2025-06-04
 */
@Slf4j
@Service
public class ActivitiesServiceImpl implements IActivitiesService {
    @Autowired
    private ActivitiesMapper activitiesMapper;

    @Autowired
    private ActivityAppMapper activityAppMapper;


    /**
     * 查询活动
     *
     * @param activityId 活动主键
     * @return 活动
     */
    @Override
    public Activities selectActivitiesByActivityId(Long activityId) {
        return activitiesMapper.selectActivitiesByActivityId(activityId);
    }

    /**
     * 查询活动列表
     *
     * @param activities 活动
     * @return 活动
     */
    @Override
    public List<Activities> selectActivitiesList(Activities activities) {
        return activitiesMapper.selectActivitiesList(activities);
    }

    /**
     * 新增活动
     *
     * @param activities 活动
     * @return 结果
     */
    @Override
    public int insertActivities(Activities activities) {
        return activitiesMapper.insertActivities(activities);
    }

    /**
     * 修改活动
     *
     * @param activities 活动
     * @return 结果
     */
    @Override
    public int updateActivities(Activities activities) {
        return activitiesMapper.updateActivities(activities);
    }

    /**
     * 批量删除活动
     *
     * @param activityIds 需要删除的活动主键
     * @return 结果
     */
    @Override
    public int deleteActivitiesByActivityIds(Long[] activityIds) {
        return activitiesMapper.deleteActivitiesByActivityIds(activityIds);
    }

    /**
     * 删除活动信息
     *
     * @param activityId 活动主键
     * @return 结果
     */
    @Override
    public int deleteActivitiesByActivityId(Long activityId) {
        return activitiesMapper.deleteActivitiesByActivityId(activityId);
    }

    @Override
    public List<ActivitiesDTO> selectActivitiesListByName(ActDTO activityName) {
        return activitiesMapper.selectActivitiesAllList(activityName);
    }

    @Override
    public TableDataInfo selectActivitiesAllList(ActDTO activities) {
        List<ActivitiesDTO> list = activitiesMapper.selectActivitiesAllList(activities);
        int total = list.size();
        //获取总页数
        // 分页处理
        int pageNum = activities.getPageNum();
        int pageSize = activities.getPageSize();
        int start = (pageNum - 1) * pageSize;
        int end = Math.min(start + pageSize, total);

        // 截取当前页的数据
        List<ActivitiesDTO> paginatedList = list.subList(start, end);

        TableDataInfo dataTable = new TableDataInfo();

        dataTable.setRows(paginatedList);
        dataTable.setTotal(total);
        dataTable.setCode(HttpStatus.SUCCESS);
        dataTable.setMsg("查询成功");


        // 设置总页数
        return dataTable;
    }

    @Override
    public List<ActivitiesDTO> listByGroup(Long[] userIds) {
        return activitiesMapper.listByGroup(userIds);
    }

    @Override
    @Transactional
    public int addActivity(ActivityVO activityVO) {
        int i = activitiesMapper.addActivity(activityVO);
        log.info("添加活动成功", activityVO.getActivityId());
        int i1 = activityAppMapper.addImages(activityVO.getActivityId(), activityVO.getImages());
        if (i > 0 && i1 > 0) {
            return 1;
        }
        return 0;
    }

    @Override
    @Transactional
    public int joinActivity(AddActivityVO addActivityVO) {
        Activities activityById = activitiesMapper.findActivityById(addActivityVO);
        if (activityById != null) {
            return -1;
        }
        // 判断活动人数是否达到
        ActivitiesDTO infoById = activitiesMapper.getInfoById(addActivityVO.getActivityId());
        if (infoById.getMaxParticipants() > infoById.getUsers().size()) {
            int i = activityAppMapper.joinActivity(addActivityVO);
            if (i > 0) {
                return 1;
            }
        }
        return 0;
    }

    @Override
    public List<Activities> selectActivitiesListAll(ActDTO activities) {
        return activitiesMapper.selectActivitiesListAll(activities);
    }

    @Override
    public ActivitiesDTO getInfoById(Long activityId) {
        ActivitiesDTO infoById = activitiesMapper.getInfoById(activityId);
        if (infoById == null) {
            return activitiesMapper.getInfoById2(activityId);
        }
        return infoById;
    }

    @Override
    public List<ActivitiesDTO> joinActivityById(Long userId, String status) {
        return activitiesMapper.joinActivityById(userId, status);
    }

    @Override
    public int publishActivity(Activities2DTO activities2DTO) {
        String location = activities2DTO.getLocation();
        SplitLocationUtil.AddressInfo addressInfo = SplitLocationUtil.parseAddress(location);
        activities2DTO.setProvince(addressInfo.getProvince());
        if (addressInfo.getCity().equals("")) {
            activities2DTO.setCity(addressInfo.getProvince());
        } else {
            activities2DTO.setCity(addressInfo.getCity());
        }
        activities2DTO.setDistrict(addressInfo.getDistrict());
        int i = activitiesMapper.publishActivity(activities2DTO);
        log.info("添加活动成功", activities2DTO.getActivityId());
        int i1 = activityAppMapper.addImages(activities2DTO.getActivityId(), activities2DTO.getImages());
        if (i > 0 && i1 > 0) {
            return 1;
        }
        return 0;
    }

    @Override
    public Integer getActiveOpen() {
        return activitiesMapper.getActiveOpen();
    }

    @Override
    public List<ActivitiesDTO> getAllActivities(ActivityTypeDTO activityTypeDTO) {
        return activitiesMapper.getAllActivities(activityTypeDTO);
    }

}